import React from "react";
import '../OtherControl/style.css';
import JTable from "@/common/Table";
import {adminDeleteGameInfo, getGameList} from "@/api/JustGameInfo";
import {gameListCol} from "@/js/columns";
import {successMsg} from "@/util/global";
import {HomeControlContainer} from "@/component/Mine/AdminConsole/HomeControl/style";

class GameControl extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            /* 多选删除框 */
            confirmVisible0: false,
            /* 单选删除框 */
            confirmVisible1: false,
            /* 多选id集合 */
            selectedRowKeys: [],
            /* 数据源 */
            data: [],
            /* 表单格式JS */
            columns: gameListCol(this),
            /* 弹框标题 */
            formTitle: '',
            /* 弹框是否显示 */
            ModalVisible: false,
            /* 表单数据 */
            formData: {},
            /* 上传文件数据 */
            fileItems: [],
        }

        this.handleDeleteIds = this.handleDeleteIds.bind(this);
        this.handleDeleteId = this.handleDeleteId.bind(this);
        this.handleLoad = this.handleLoad.bind(this);
        this.handleClickEdit = this.handleClickEdit.bind(this);
        this.handleShowConfirm = this.handleShowConfirm.bind(this);
        this.handleClickAdd = this.handleClickAdd.bind(this);
        this.handleCancel = this.handleCancel.bind(this);
        this.onSelectChange = this.onSelectChange.bind(this);
    }

    /* 单条删除 */
    handleDeleteId() {
        this.handleDelete({ids: [this.state.selectedId]})
    }

    /* 多条删除 */
    handleDeleteIds() {
        this.handleDelete({ids: this.state.selectedIds})
    }

    handleDelete(params) {
        adminDeleteGameInfo(params).then(response => {
            if (response.success) {
                let params = {
                    page: this.refs.childTable.state.page,
                    size: this.refs.childTable.state.size
                };
                this.handleLoad(params);
                this.handleCancel();
                successMsg("操作成功！");
            }
        })
    }


    /* 多选触发 */
    onSelectChange = (ids, rows) => {
        let finalState = {
            selectedRowKeys: rows,
            selectedIds: ids,
            disableClick: rows.length > 1
        }
        this.setState({...finalState});
    };

    handleClickEdit(data) {
        window._ROUTER_.push({pathname: '/edit-game', query: {gameInfoId: data.id}});
    }

    handleShowConfirm(id) {
        if (id) {
            this.setState({
                selectedId: id,
                confirmVisible1: true
            })
        } else {
            this.setState({
                confirmVisible0: true
            })
        }
    }

    /* 弹框表单点击提交 */
    handleClickAdd() {
        window._ROUTER_.push({pathname: '/upload-game'});
    }

    componentWillUnmount() {
        this.setState({
            confirmVisible0: true
        })
    }

    /* 弹框表单点击取消 */
    handleCancel() {
        this.setState({
            formData: {},
            fileItems: [],
            ModalVisible: false,
            confirmVisible0: false,
            confirmVisible1: false,
        });
    }

    /* 需要加载数据时触发的方法 */
    handleLoad(params) {
        getGameList(params).then(res => {
            if (res.success) {
                this.setState({
                    data: res.data
                })
            }
        })
    }

    render() {
        return <HomeControlContainer>
            <JTable ref={"childTable"}
                    data={this.state.data}
                    columns={this.state.columns}
                    onReload={this.handleLoad}
                    defaultPage={1}
                    defaultSize={10}
                    onDelete={this.handleDelete}
                    clickSaveCallback={this.handleClickAdd}
            />
        </HomeControlContainer>
    }
}

export default GameControl;
